package com.day0916.task;

import java.io.IOException;
import java.util.Scanner;

public class LoginManager {
    Scanner in = new Scanner(System.in);

    public boolean register() throws IOException {
        FileManager fileManager = new FileManager();
        String jsonStr = fileManager.getJsonStr();
        String file_userName = fileManager.getValue(jsonStr,"user_name");
        //System.out.println(userName);
        String file_userPwd = fileManager.getValue(jsonStr,"user_password");
        //System.out.println(userPwd);
        User user = new User();
        int allow_count = 3;
        do {
            System.out.print("请输入用户名：");
            String username = in.next();
            user.user_name = username;
            System.out.print("请输入密码：");
            String userpassword = in.next();
            user.user_password = userpassword;
            //login(user);
            if(file_userName.equals(user.user_name)){
                if(file_userPwd.equals(user.user_password)){
                    System.out.println("登录成功！");
                    return true;
                }
            }
            System.out.println("登录失败！");
            allow_count--;
            if (allow_count != 0){
                System.out.println("你还有" + allow_count + "次机会！");
            }
            else {
                System.out.println("账户已锁定！");
            }
        } while (allow_count > 0);
        return false;
    }

//    public void login(User user){
//            if("admin".equals(user.user_name)){
//                if("123456".equals(user.user_password)){
//                    System.out.println("登录成功！");
//                    return;
//                }
//            }
//            System.out.println("登录失败！");
//    }

    public static void main(String[] args) throws IOException {
//        以JSON格式保存在文本文件中

        LoginManager loginManager = new LoginManager();
        loginManager.register();
        FileManager fileManager = new FileManager();
        fileManager.file();

    }
}
